package com.iotmonitor.iot.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.iotmonitor.common.core.utils.poi.ExcelUtil;
import com.iotmonitor.iot.domain.TbQuota;
import com.iotmonitor.iot.emq.EmqClient;
import com.iotmonitor.iot.service.ITbQuotaService;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.iotmonitor.common.log.annotation.Log;
import com.iotmonitor.common.log.enums.BusinessType;
import com.iotmonitor.common.security.annotation.RequiresPermissions;
import com.iotmonitor.common.core.web.controller.BaseController;
import com.iotmonitor.common.core.web.domain.AjaxResult;
import com.iotmonitor.common.core.web.page.TableDataInfo;

/**
 * 指标配置Controller
 * 
 * @author ruoyi
 * @date 2023-08-11
 */
@RestController
@RequestMapping("/quota")
public class TbQuotaController extends BaseController
{
    @Autowired
    private ITbQuotaService tbQuotaService;

    @Autowired
    private EmqClient emqClient;

    /**
     * 查询指标配置列表
     */
    @RequiresPermissions("system:quota:list")
    @GetMapping("/list")
    public TableDataInfo list(TbQuota tbQuota)
    {
        startPage();
        List<TbQuota> list = tbQuotaService.selectTbQuotaList(tbQuota);
        return getDataTable(list);
    }

    /**
     * 分页获取数值型指标
     * @return
     */
    @GetMapping("/numberQuota")
    public TableDataInfo queryNumberQuota(TbQuota tbQuota){
        startPage();
        List<TbQuota> list = tbQuotaService.queryNumberQuota(tbQuota);
        return getDataTable(list);
    }

    /**
     * 导出指标配置列表
     */
    @RequiresPermissions("system:quota:export")
    @Log(title = "指标配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbQuota tbQuota)
    {
        List<TbQuota> list = tbQuotaService.selectTbQuotaList(tbQuota);
        ExcelUtil<TbQuota> util = new ExcelUtil<TbQuota>(TbQuota.class);
        util.exportExcel(response, list, "指标配置数据");
    }

    /**
     * 获取指标配置详细信息
     */
    @RequiresPermissions("system:quota:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbQuotaService.selectTbQuotaById(id));
    }

    /**
     * 新增指标配置
     */
    @RequiresPermissions("system:quota:add")
    @Log(title = "指标配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated TbQuota tbQuota)
    {
        List<TbQuota> tbQuotas = tbQuotaService.selectTbQuotaList(new TbQuota(tbQuota.getSubject()));
        if (!tbQuotas.isEmpty()){
            return error("已存在该名称");
        }
        //订阅主题
        try {
            emqClient.subscribe("$queue/"+tbQuota.getSubject());
        } catch (MqttException e) {
            e.printStackTrace();
        }
        return toAjax(tbQuotaService.insertTbQuota(tbQuota));
    }

    /**
     * 修改指标配置
     */
    @RequiresPermissions("system:quota:edit")
    @Log(title = "指标配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated TbQuota tbQuota)
    {
        return toAjax(tbQuotaService.updateTbQuota(tbQuota));
    }

    /**
     * 删除指标配置
     */
    @RequiresPermissions("system:quota:remove")
    @Log(title = "指标配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbQuotaService.deleteTbQuotaByIds(ids));
    }

    /**
     * 删除指标
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        return tbQuotaService.deleteTbQuotaById(id) > 0 ? AjaxResult.success():AjaxResult.error("该指标使用中");
    }
}
